package org.example.day06;


import sun.awt.windows.ThemeReader;

/**
 * 第二章节:2.3.1.2：解决同步死循环 (解决2.3.1的死循环)
 *      一个线程修改了另一个线程中的值。
 *      两个线程都操作m2对象的属性
 */
public class MyThread2 implements Runnable{
    private boolean isContinuePrint = true;

    public boolean isContinuePrint() {
        return isContinuePrint;
    }

    public void setContinuePrint(boolean continuePrint) {
        isContinuePrint = continuePrint;
    }

    public void printMethod(){
        try {
            while(isContinuePrint == true){
                System.out.println("threadName = " + Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        System.out.println(this);
        System.out.println(Thread.currentThread().getName());
        printMethod();
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread2 m2 = new MyThread2();
        //注意 new MyThread2().start() 和 new Thread(m2).start(); 的区别。
        //new MyThread2().start()是开启MyThread2线程，执行MyThread2对象中的run方法
        //new Thread(m2).start();是开启另外一个线程，执行MyThread2对象中的run方法
        new Thread(m2).start();
        Thread.sleep(100);
        System.out.println("停止！~" + Thread.currentThread().getName());
        m2.setContinuePrint(false);
    }
}
